package com.ziq.base.transition.custom

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Parcel
import android.os.Parcelable
import android.transition.Transition
import android.transition.TransitionValues
import android.util.Property
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.ziq.base.transition.ShareElementInfo
import com.ziq.base.transition.ShareElementInfoDataUpdate

class ChangeTextTransition : Transition() {

    companion object{
        const val PROPNAME_TEXTSIZE = "ChangeTextTransition::textSize"
        const val PROPNAME_TEXTCOLOR = "ChangeTextTransition::textColor"
    }


    init {
        addTarget(TextView::class.java)
    }

    override fun captureStartValues(transitionValues: TransitionValues) {
        val info = ShareElementInfo.ShareElementInfoData.getFromView(transitionValues.view)
        //注意 此时 还不能确定 info?.customFromData  需要等到 onSharedElementEnd 后才知道 进出方向
        val properties = if (info?.isEnter == true) info.customFromData?.properties else info?.customToData?.properties
        if(properties?.containsKey(PROPNAME_TEXTSIZE) == true && properties.containsKey(PROPNAME_TEXTCOLOR)){
            println("ChangeTextTransition start View ${transitionValues.view.hashCode()} size = ${properties.get(PROPNAME_TEXTSIZE)} color = ${properties.get(PROPNAME_TEXTCOLOR)}")
        }
    }

    override fun captureEndValues(transitionValues: TransitionValues) {
        val info = ShareElementInfo.ShareElementInfoData.getFromView(transitionValues.view)
        val properties = if (info?.isEnter == true) info.customToData?.properties else info?.customFromData?.properties
        if(properties?.containsKey(PROPNAME_TEXTSIZE) == true && properties.containsKey(PROPNAME_TEXTCOLOR)){
            println("ChangeTextTransition end View ${transitionValues.view.hashCode()} size = ${properties.get(PROPNAME_TEXTSIZE)} color = ${properties.get(PROPNAME_TEXTCOLOR)}")
        }
    }

    override fun createAnimator(
        sceneRoot: ViewGroup,
        startValues: TransitionValues,
        endValues: TransitionValues
    ): Animator? {
        val info = ShareElementInfo.ShareElementInfoData.getFromView(endValues.view)
        if(info?.customFromData?.properties?.containsKey(PROPNAME_TEXTSIZE) == true  && info.customToData?.properties?.containsKey(PROPNAME_TEXTSIZE) == true
            && info.customFromData?.properties?.containsKey(PROPNAME_TEXTCOLOR) == true  && info.customToData?.properties?.containsKey(PROPNAME_TEXTCOLOR) == true
        ){
            val startTextSize:Float? = info.customFromData?.properties?.get(PROPNAME_TEXTSIZE) as Float?
            val endTextSize:Float? = info.customToData?.properties?.get(PROPNAME_TEXTSIZE) as Float?
            val startTextColor:Int? = info.customFromData?.properties?.get(PROPNAME_TEXTCOLOR) as Int?
            val endTextColor:Int? = info.customToData?.properties?.get(PROPNAME_TEXTCOLOR) as Int?
            println("ChangeTextTransition  createAnimator size:${startTextSize} -> ${endTextSize} color:${startTextColor} -> ${endTextColor}")
            if(startTextSize != null && endTextSize != null && startTextColor != null && endTextColor != null){
                val view = endValues.view as TextView
                val textSizeAnimator: ObjectAnimator = ObjectAnimator.ofFloat<TextView>(
                    view,
                    TextSizeProperty(),
                    startTextSize,
                    endTextSize
                )
                val textColorAnimator: ObjectAnimator = ObjectAnimator.ofArgb<TextView>(
                    view,
                    TextColorProperty(),
                    startTextColor,
                    endTextColor
                )
                val animatorSet = AnimatorSet()
                animatorSet.playTogether(textSizeAnimator, textColorAnimator)
                return animatorSet
            }
        }
        return null
    }

    class TextData() : ShareElementInfoDataUpdate() {

        constructor(parcel: Parcel) : this() {
            properties = parcel.readBundle(javaClass.classLoader)
        }

        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeBundle(properties)
        }

        override fun update(view: View?) {
            if(view is TextView){
                properties?.putFloat(PROPNAME_TEXTSIZE, view.textSize)
                properties?.putInt(PROPNAME_TEXTCOLOR, view.currentTextColor)
            }
        }

        override fun describeContents(): Int {
            return 0
        }

        companion object CREATOR : Parcelable.Creator<TextData> {
            override fun createFromParcel(parcel: Parcel): TextData {
                return TextData(parcel)
            }

            override fun newArray(size: Int): Array<TextData?> {
                return newArray(size)
            }
        }

    }

    private class TextSizeProperty : Property<TextView, Float>(Float::class.java, "textSize") {
        override fun set(view: TextView, value: Float?) {
            view.setTextSize(TypedValue.COMPLEX_UNIT_PX, value!!)
        }

        override fun get(view: TextView): Float {
            return view.textSize
        }
    }

    private class TextColorProperty : Property<TextView, Int>(Int::class.java, "textColor") {
        override fun set(view: TextView, value: Int?) {
            view.setTextColor(value!!)
        }

        override fun get(view: TextView): Int {
            return view.currentTextColor
        }
    }

}